/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.jSkyRoom.bridge;

import java.util.Collection;
import ru.jSkyRoom.bridge.major.MajorBridge;
import ru.jSkyRoom.interfaces.JSkyMod;
import ru.jSkyRoom.interfaces.major.Major;
import skyproc.FormID;
import skyproc.GRUP_TYPE;
import skyproc.MajorRecord;
import skyproc.Mod;

/**
 *
 * @author Rakev
 */
public class ModBridge extends JSkyBridge<Mod> implements JSkyMod {

    public ModBridge(Mod adapting) {
        super(adapting);
    }

    @Override
    public void addMajor(Major major) {
        if (major instanceof MajorBridge) {
            MajorBridge majorBridge = (MajorBridge) major;
            getRawObject().addRecord((MajorRecord) majorBridge.getRawObject());

        } else {
            throw new IllegalArgumentException("Only bridge supported.");
        }
    }

    @Override
    public void removeMajor(Major major) {
        getRawObject().remove(major.getForm());
    }

    @Override
    public <T extends Major> T makeCopy(T copiedMajor, String newEDID) {
        if (copiedMajor instanceof MajorBridge) {
            MajorBridge bridge = (MajorBridge) copiedMajor;
            MajorRecord templateRecord = (MajorRecord) bridge.getRawObject();
            MajorRecord copyRecord = getRawObject().makeCopy(templateRecord, newEDID);
            return (T) getBridgeFactory().createBridge(copyRecord, bridge.getClass());
        }
        throw new IllegalArgumentException("Support only MajorBridge");
    }

    @Override
    public void addAsOverrides(JSkyMod mod, GRUP_TYPE... grupTypes) {
        if (mod instanceof ModBridge) {
            Mod addingMod = ((ModBridge) mod).getRawObject();
            getRawObject().addAsOverrides(addingMod, grupTypes);
            return;
        }
        throw new IllegalArgumentException("Support only MajorBridge");

    }

    @Override
    public Major getMajor(FormID id, GRUP_TYPE... lookingGrups) {
        MajorRecord findedRecord = getRawObject().getMajor(id, lookingGrups);
        if (findedRecord != null) {
            return (Major) getBridgeFactory().createBridge(findedRecord);
        }
        return null;
    }

    @Override
    public Major getMajor(String editId, GRUP_TYPE... lookingGrups) {
        MajorRecord findedRecord = getRawObject().getMajor(editId, lookingGrups);
        if (findedRecord != null) {
            return (Major) getBridgeFactory().createBridge(findedRecord);
        }
        return null;
    }

    @Override
    public boolean contains(Major major) {
        return getRawObject().contains(major.getForm());
    }

    @Override
    public Collection<Major> getMajors(GRUP_TYPE grupType) {
        Collection<MajorRecord> records = getRawObject().getGRUPs().get(grupType).getRecords();
        return (Collection) getBridgeFactory().createBridges(records);
    }

    @Override
    public String getName() {
        return getRawObject().getInfo().print();
    }
}
